package com.example.dellpc.myapplication;

import android.app.Activity;
import android.graphics.drawable.ClipDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ImageView;

import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;

/**
 * Created by 刘智强 on 2016/10/12.
 *  ClipDrawable的使用demo
 */
public class ClipDrawableActivity extends Activity {
    @ViewInject(R.id.iv_clip)
    private ImageView iv_clip;

    private  ClipDrawable clipDrawable;
    private boolean flag=false;
    private int level=0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_clipdrawable);
        ViewUtils.inject(this);
        init();
    }

    public  void init(){
        clipDrawable= (ClipDrawable) iv_clip.getDrawable();
        final Handler handler=new Handler(){
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
            }
        };
        final Runnable runnable1=new Runnable() {
            @Override
            public void run() {
                handler.postDelayed(this,200);
                level+=200;
                clipDrawable.setLevel(level);
                if (level>=10000){
                    handler.removeCallbacks(this);
                }


            }
        };
        final Runnable runnable2=new Runnable() {
            @Override
            public void run() {
                handler.postDelayed(this,200);
                level-=200;
                clipDrawable.setLevel(level);
                if (level<=0){
                    handler.removeCallbacks(this);
                }


            }
        };

        iv_clip.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {




                if (flag){
                    handler.postDelayed(runnable2,200);
                    handler.removeCallbacks(runnable1);
                    flag=false;
                }else {
                    flag=true;
                    handler.postDelayed(runnable1,200);
                    handler.removeCallbacks(runnable2);

                }
            }
        });

    }

}
